package com.jstyle.jclife.activity;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.jstyle.jclife.R;
import com.jstyle.jclife.adapter.HistoryDateAdapter;
import com.jstyle.jclife.adapter.HistoryShowAdapter;
import com.jstyle.jclife.fragment.ExerciseFragment;
import com.jstyle.jclife.fragment.SleepDayFragment;
import com.jstyle.jclife.fragment.SleepMonthFragment;
import com.jstyle.jclife.fragment.SleepWeekFragment;
import com.jstyle.jclife.utils.ScreenUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SleepHistoryActivity extends BaseActivity {

    private static final int FRAGMENT_SLEEP_DAY = 0;
    private static final int FRAGMENT_SLEEP_WEEK = 1;
    private static final int FRAGMENT_SLEEP_MONTH = 2;
    @BindView(R.id.bt_goal_back)
    Button btGoalBack;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;

    @BindView(R.id.rb_sleep_day)
    RadioButton rbSleepDay;
    @BindView(R.id.rb_sleep_week)
    RadioButton rbSleepWeek;
    @BindView(R.id.rb_sleep_month)
    RadioButton rbSleepMonth;
    @BindView(R.id.rg_history)
    RadioGroup rgHistory;
    private static final String TAG = "SleepHistoryActivity";
    private HistoryDateAdapter historyDateAdapter;
    private HistoryShowAdapter historyShowAdapter;
    private List<String> listContent = new ArrayList<>();
    private String[] showWeek;
    private String queryDate;
    private int position;
    SleepDayFragment sleepDayFragment;
    SleepWeekFragment sleepWeekFragment;
    SleepMonthFragment sleepMonthFragment;
    private FragmentManager fragmentManager;
    int FRAGMENT_FLAG=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sleep_history);
        ButterKnife.bind(this);
        init();
    }


    private void init() {
        queryDate=getIntent().getStringExtra(ExerciseFragment.KEY_QUERYDATE);
        ScreenUtils.setBoldTitleTypeface(btGoalBack);
        fragmentManager=getSupportFragmentManager();
        startTransaction(FRAGMENT_SLEEP_DAY);
        rgHistory.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int fragmentId=0;
                switch (checkedId){
                    case R.id.rb_sleep_day:
                        fragmentId=FRAGMENT_SLEEP_DAY;
                        break;
                    case R.id.rb_sleep_week:
                        fragmentId=FRAGMENT_SLEEP_WEEK;
                        break;
                    case R.id.rb_sleep_month:
                        fragmentId=FRAGMENT_SLEEP_MONTH;
                        break;
                }
                startTransaction(fragmentId);
            }
        });
    }
    protected void startTransaction(int fragmentId) {
        FRAGMENT_FLAG=fragmentId;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (fragmentId) {
            case FRAGMENT_SLEEP_DAY:
                if (sleepDayFragment == null) {
                    sleepDayFragment =SleepDayFragment.newInstance(queryDate,"");
                    fragmentTransaction.add(R.id.frameLayout_content, sleepDayFragment);
                } else {
                    fragmentTransaction.show(sleepDayFragment);
                }
                break;
            case FRAGMENT_SLEEP_WEEK:
                if (sleepWeekFragment == null) {
                    sleepWeekFragment = new SleepWeekFragment();
                    fragmentTransaction.add(R.id.frameLayout_content, sleepWeekFragment);
                } else {
                    fragmentTransaction.show(sleepWeekFragment);
                }
                break;
            case FRAGMENT_SLEEP_MONTH:
                if (sleepMonthFragment == null) {
                    sleepMonthFragment = new SleepMonthFragment();
                    fragmentTransaction.add(R.id.frameLayout_content, sleepMonthFragment);
                } else {
                    fragmentTransaction.show(sleepMonthFragment);
                }
                break;

        }

        fragmentTransaction.commit();
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        hideFragment(sleepDayFragment, fragmentTransaction);
        hideFragment(sleepMonthFragment, fragmentTransaction);
        hideFragment(sleepWeekFragment, fragmentTransaction);

    }

    private void hideFragment(Fragment fragment, FragmentTransaction transaction) {
        if (fragment != null) {
            transaction.hide(fragment);
        }
    }
    @OnClick({R.id.iv_back, R.id.iv_share})
    public void onViewClicked(View view) {

        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_share:
                share();
                break;

        }

    }

    private void share() {
        if (FRAGMENT_FLAG == FRAGMENT_SLEEP_DAY) {
            sleepDayFragment.shoot();
        } else if (FRAGMENT_FLAG == FRAGMENT_SLEEP_MONTH) {
            sleepMonthFragment.shoot();
        } else if (FRAGMENT_FLAG == FRAGMENT_SLEEP_WEEK) {
            sleepWeekFragment.shoot();
        }
    }


}
